package com.tianpengtech.front.web.task;

import com.jfinal.kit.PropKit;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.ehcache.CacheKit;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import com.tianpengtech.admin.dictionary.DictionaryDao;
import com.tianpengtech.admin.task.SysConfigDao;
import com.tianpengtech.common.exception.ValidateException;
import com.tianpengtech.common.im.kit.PushKit;
import com.tianpengtech.common.ip.IpInfo;
import com.tianpengtech.common.ip.IpUtil;
import com.tianpengtech.common.model.authenticate.Authenticate;
import com.tianpengtech.common.model.dictionary.Dictionary;
import com.tianpengtech.common.model.fair.Fair;
import com.tianpengtech.common.model.fairauth.FairAuth;
import com.tianpengtech.common.model.fairorder.FairOrder;
import com.tianpengtech.common.model.fairsalary.FairSalary;
import com.tianpengtech.common.model.moneylog.MoneyLog;
import com.tianpengtech.common.model.task.*;
import com.tianpengtech.common.model.user.User;
import com.tianpengtech.common.search.EqualCondition;
import com.tianpengtech.common.search.LikeCondition;
import com.tianpengtech.common.search.QueryConstructor;
import com.tianpengtech.common.util.DateUtils;
import com.tianpengtech.common.util.SendSmsMsg;
import com.tianpengtech.common.util.ToolRandoms;
import com.tianpengtech.common.util.UAUtil;
import com.tianpengtech.common.validator.Validator;
import com.tianpengtech.common.validator.rules.numberic.MustBeNumberic;
import com.tianpengtech.common.validator.rules.numberic.NumbericMustBeEqual;
import com.tianpengtech.common.validator.rules.numberic.NumbericMustBeGreaterEqualThan;
import com.tianpengtech.common.validator.rules.numberic.NumbericMustBeGreaterThan;
import com.tianpengtech.common.validator.rules.numberic.NumbericMustBeLessEqualThan;
import com.tianpengtech.common.validator.rules.numberic.NumbericMustBeLessThan;
import com.tianpengtech.common.validator.rules.string.StringLengthBetween;
import com.tianpengtech.common.validator.rules.string.StringMustBeRequired;
import com.tianpengtech.front.web.fair.FairService;
import com.tianpengtech.front.web.login.LoginService;
import cz.mallat.uasparser.OnlineUpdater;
import cz.mallat.uasparser.UASparser;
import cz.mallat.uasparser.UserAgentInfo;

import java.util.*;


/**
 * Created by Administrator on 2017/5/31 0031.
 */
public class TaskService {
    private static Task taskDao = Task.dao;
    private static FairService fairService = new FairService();
    private static String TASK_CACHE_CLICK_NAME = "task_click";
    private final Authenticate authDao = Authenticate.dao;
    private final TaskSource taskSourceDao = TaskSource.dao;

    private static String PARAM_TASK_NAME="taskName";
    private static String PARAM_TASK_STATUS="taskStatus";

    private static String PARAM_TASK_STATUS_BEFORE="1"; //未开始
    private static String PARAM_TASK_STATUS_AFTER="2";  //进行中
    private static String PARAM_TASK_STATUS_END="3";    //已完成

    /**
     * 保存任务
     * @param task
     * @throws ValidateException 
     */
    public void save(Task task,String[] source) throws ValidateException{
        
    	int h5_type=task.getInt(Task.H5_TYPE);
    	float price= task.getFloat(Task.TASK_PRICE);
    	int extension_type=task.getInt(Task.EXTENSION_TYPE);
    	String sql="select * from "+Authenticate.dao.tableName()+" where "+Authenticate.USER_ID+" =?";
    	Authenticate auth = Authenticate.dao.findFirst(sql, task.getLong(Task.CREATE_USER));
    	
    	Date start = task.getDate(Task.TASK_START_TIME);
    	Date end = task.getDate(Task.TASK_END_TIME);
    	
    	Date now = new Date();
    	Validator validator = new Validator();
    	int status = (auth==null)?Authenticate.STATUS_FAILURE:auth.getInt(Authenticate.STATUS);
    	validator.addRule(new NumbericMustBeEqual(status, Authenticate.STATUS_SUCCESS, "当前用户资质认证审核未通过，不允许提交任务"));
    	validator.addRule(new StringLengthBetween(task.getStr(Task.TASK_TITLE),"任务标题只能是1-15个字符",1,15));
    	
    	
    	validator.addRule(new StringMustBeRequired(DateUtils.formateData(start, DateUtils.YYMMdd_FORMAT),"任务开始时间不能为空"));
    	validator.addRule(new StringMustBeRequired(DateUtils.formateData(end,DateUtils.YYMMdd_FORMAT),"任务结束时间不能为空"));
    	validator.addRule(new NumbericMustBeGreaterEqualThan(end.getTime(),start.getTime(),"任务结束日期必须在开始日期之后"));
    	validator.addRule(new NumbericMustBeGreaterEqualThan(start.getTime(),now.getTime(),"任务开始时间必须在今天日期之后"));
    	
    	validator.addRule(new StringMustBeRequired(price+"","任务酬劳不能为空"));
    	validator.addRule(new NumbericMustBeGreaterEqualThan((int)price,0,"请设置任务酬劳"));
    	validator.addRule(new StringLengthBetween(task.getStr(Task.TASK_DESCRIPTION),"任务描述不能超过400字符",1,400));
    	
    	if(extension_type==Task.EXT_TYPE_ZD){
    		validator.addRule(new NumbericMustBeLessEqualThan(task.getInt(Task.WH_NUMBER),2000,"主播数量不能超过2000个"));
    	}
    	
    	if(h5_type==2){
    		validator.addRule(new NumbericMustBeGreaterThan(source.length,0,"请上传资源图片"));
    	}
    	validator.excute();
    	
    	if (task.getInt(Task.EXTENSION_TYPE)==Task.EXT_TYPE_ZD){
            task.set(Task.STATUS,Task.STATUS_WAIT);
        }else{
            task.set(Task.STATUS,Task.STATUS_APPROVING);
        }
    	
        task.save();
        if (source!=null && source.length>0){
            for (int i=0;i<source.length;i++) {
                String s=source[i];
                TaskSource ts = new TaskSource();
                ts.set(TaskSource.TASK_ID,task.get(Task.ID));
                ts.set(TaskSource.SOURCE,s);
                ts.set(TaskSource.SORT,i+1);
                ts.set(TaskSource.CREATE_DATE,new Date());
                ts.save();
            }
        }
        
    }

    /**
     * 选择完网红后保存
     */
    public Ret saveTaskWait(String[] fairs,String taskId){
        if (!fairService.isExistFair(fairs)){
            return Ret.fail("msg","网红ID不存在");
        }
        for (String id:fairs) {
            if (isExistFairTask(id,taskId)){
                continue;
            }
            Record record = new Record();
            record.set(Task.WL_FAIR_ID,id).set(Task.WL_TASK_ID,taskId);
            Db.save(Task.TASK_WHITELIST,record);
            TaskWh tw= new TaskWh();
            tw.set(TaskWh.WH_ID,id);
            tw.set(TaskWh.TASK_ID,taskId);
            tw.set(TaskWh.CODE,getTaskCode());
            tw.set(TaskWh.STATUS,Task.STATUS_START);
            tw.save();
        }
        Task task=taskDao.findById(taskId);
        
        if(task.getInt(Task.STATUS)==Task.STATUS_WAIT){
        	 task.set(Task.STATUS,Task.STATUS_APPROVING);
        }
        task.update();
        return Ret.ok();
    }

    /**
     * 判断网红是否已经领取过此任务
     * @param id
     * @return
     */
    public static boolean isExistFairTask(String id,String taskID){
        String key="isExist_"+id+"_"+taskID;
        Boolean f=CacheKit.get(TaskWh.TASKWH_CACHE_NAME,key);
        if (f!=null){
            return f;
        }
        List<TaskWh> list= queryFairTask(id,taskID);
        if (list.size()>0){
            return true;
        }else{
            return false;
        }
    }

    
    public static List<TaskWh>  queryFairTask(String id,String taskID){
    	 String sql = "select tw.id from "+TaskWh.dao.tableName()+" tw  where "+TaskWh.TASK_ID+ "=? and "+TaskWh.WH_ID+"=? and tw."+TaskWh.DELETETAG+"="+TaskWh.DELETE_STATUS_NO;
         List<TaskWh> list=TaskWh.dao.find(sql,new Object[]{taskID,id});
         return list;
    }

    /**
     * 点击任务链接保存
     * @param ref
     * @param ua
     * @param ip
     */
    public void saveTaskClick(String ref, String ua ,String ip,String code){
        synchronized (code) {
            TaskWh taskWh = getByCode(code);
//            boolean devModel = PropKit.getBoolean("devMode");
            boolean devModel = getTaskCountMode();
            
            if(taskWh==null){
            	return ;
            }
            if (!devModel) {//假如是真实环境，则只记录真实的点击数
                Object i = CacheKit.get(TASK_CACHE_CLICK_NAME, ip + taskWh.get(TaskWh.ID));
                if (i != null) {//假如当前记录再有效期内，则不记录当前数据
                	return;
                }
                updateRealClickNum(ref, ua, ip, code, taskWh);
            }else{
            	updateRealClickNum(ref, ua, ip, code, taskWh);
            	updateDevClickNum(ref, ua, ip, code, taskWh,TaskCount.TYPE_DEV);
            }
            
        }
    }

	private void updateRealClickNum(String ref, String ua, String ip, String code, TaskWh taskWh) {
		updateDevClickNum(ref, ua, ip, code, taskWh,TaskCount.TYPE_PRO);
		//缓存内写入数据防止此IP在限制时间内再次点击
		CacheKit.put(TASK_CACHE_CLICK_NAME, ip + taskWh.get(TaskWh.ID), 1);
		//点击次数加一
		int num = taskWh.get(TaskWh.CLICK_NUM);
		taskWh.set(TaskWh.CLICK_NUM, ++num);
		taskWh.update();
	}

	private void updateDevClickNum(String ref, String ua, String ip, String code, TaskWh taskWh,int type) {
		UserAgentInfo userAgentInfo = UAUtil.get(ua);
		IpInfo info = IpUtil.getIpInfo(ip);
		TaskCount taskCount = new TaskCount();
		taskCount.set(TaskCount.TASK_ID, taskWh.get(TaskWh.TASK_ID));
		taskCount.set(TaskCount.WH_ID, taskWh.get(TaskWh.WH_ID));
		taskCount.set(TaskCount.URL, ref);
		taskCount.set(TaskCount.IP, ip);
		taskCount.set(TaskCount.TASK_CODE, code);
		taskCount.set(TaskCount.USER_AGENT, ua);
		taskCount.set(TaskCount.TYPE,type);
		if (userAgentInfo != null) {
		    taskCount.set(TaskCount.OS_NAME, userAgentInfo.getOsName());
		    taskCount.set(TaskCount.UA_NAME, userAgentInfo.getUaName());
		    taskCount.set(TaskCount.DEVICE_TYPE, userAgentInfo.getDeviceType());
		    taskCount.set(TaskCount.REQ_TYPE, userAgentInfo.getType());
		}
		if (info != null) {
		    taskCount.set(TaskCount.COUNTRY, info.getCountry());
		    taskCount.set(TaskCount.PART, info.getPart());
		    taskCount.set(TaskCount.PROVINCE, info.getProvince());
		    taskCount.set(TaskCount.CITY, info.getCity());
		}
		taskCount.save();
	}


    /**
     * 点击任务链接保存
     * @param ref
     * @param ua
     * @param ip
     */
    public void saveTaskCvs(String ref, String ua ,String ip,String code){
        synchronized (code) {
            TaskWh taskWh = getByCode(code);
//            boolean devModel = PropKit.getBoolean("devMode");
            boolean devModel = getTaskCountMode();
            if(taskWh==null){
            	return;
            }
            //如果是测试环境
            if (!devModel) {
                Object i = CacheKit.get(TASK_CACHE_CLICK_NAME, ip + taskWh.get(TaskWh.ID));
                //判断在缓存有效期内是否点击过
                if (i != null) {
                    return;
                }
                updateRealCVS(ref, ua, ip, code, taskWh,TaskCount.TYPE_PRO);
            }else{
            	updateRealCVS(ref, ua, ip, code, taskWh,TaskCount.TYPE_PRO);
            	updateDevCVS(ref, ua, ip, code, taskWh,TaskCount.TYPE_DEV);
            }
        }
    }

	private void updateRealCVS(String ref, String ua, String ip, String code, TaskWh taskWh,int type) {
		updateDevCVS(ref, ua, ip, code, taskWh, type);
		//缓存内写入数据防止此IP在限制时间内再次点击
		CacheKit.put(TASK_CACHE_CLICK_NAME, ip + taskWh.get(TaskWh.ID), 1);
		//点击次数加一
		int num = taskWh.get(TaskWh.CVS_NUM);
		taskWh.set(TaskWh.CVS_NUM, ++num);
		taskWh.update();
	}

	private void updateDevCVS(String ref, String ua, String ip, String code, TaskWh taskWh, int type) {
		UserAgentInfo userAgentInfo = UAUtil.get(ua);
		IpInfo info = IpUtil.getIpInfo(ip);
		TaskCVS taskCvs = new TaskCVS();
		taskCvs.set(TaskCount.TASK_ID, taskWh.get(TaskWh.TASK_ID));
		taskCvs.set(TaskCount.WH_ID, taskWh.get(TaskWh.WH_ID));
		taskCvs.set(TaskCount.URL, ref);
		taskCvs.set(TaskCount.IP, ip);
		taskCvs.set(TaskCount.TASK_CODE, code);
		taskCvs.set(TaskCount.USER_AGENT, ua);
		taskCvs.set(TaskCount.TYPE, type);
		if (userAgentInfo != null) {
		    taskCvs.set(TaskCount.OS_NAME, userAgentInfo.getOsName());
		    taskCvs.set(TaskCount.UA_NAME, userAgentInfo.getUaName());
		    taskCvs.set(TaskCount.DEVICE_TYPE, userAgentInfo.getDeviceType());
		    taskCvs.set(TaskCount.REQ_TYPE, userAgentInfo.getType());
		}
		if (info != null) {
		    taskCvs.set(TaskCount.COUNTRY, info.getCountry());
		    taskCvs.set(TaskCount.PART, info.getPart());
		    taskCvs.set(TaskCount.PROVINCE, info.getProvince());
		    taskCvs.set(TaskCount.CITY, info.getCity());
		}
		taskCvs.save();
	}



    /**
     * 获取唯一Code
     * @return
     */
    private String getTaskCode(){
        String code=ToolRandoms.getUuid(true);
        TaskWh record=TaskWh.dao.findFirstByCache(Task.TASK_CACHE_NAME,code,"select * from "+TaskWh.dao.tableName() + " where code=?",code);
        if (record==null) {
            CacheKit.put(Task.TASK_CACHE_NAME,code,record);
            return code;
        }else{
            return getTaskCode();
        }
    }


    /**
     * 根据Code 获取值
     * @return
     */
    public TaskWh getByCode(String code){
        TaskWh record=TaskWh.dao.findFirst("select * from "+TaskWh.dao.tableName() + " where code=?",code);
        return record;
    }


    /**
     * 分页查询
     * @param pageNum
     * @param length
     * @param params
     * @return
     */
    public Page<Task> task(int pageNum, int length, Map<String,Object> params){
        List<Object> param=new ArrayList<>();
        String s = "select *,task.status as status,task.id as id ";
        StringBuffer sql= new StringBuffer(" from " + taskDao.tableName() + " task left join " + TaskWh.dao.tableName() + " wh on task." + Task.ID + "=wh." + TaskWh.TASK_ID +" left join "+Fair.dao.tableName()+" fair on fair."+Fair.ID+"=wh."+TaskWh.WH_ID);
        sql.append(" where task." + Task.DELETETAG+"=? and task."+Task.CREATE_USER+"=? and task."+Task.STATUS+"!="+Task.STATUS_WAIT );
        param.add(Task.DELETE_STATUS_NO);
        param.add(params.get("user"));
        if (params!=null) {
            Object taskName = params.get(PARAM_TASK_NAME);
            Object taskStatus = params.get(PARAM_TASK_STATUS);
            if (taskName!=null && StrKit.notBlank(taskName.toString())) {
                sql.append(" and (");
                sql.append("task." + Task.TASK_TITLE+" like ? or ");
                param.add("%" + taskName.toString() + "%");
                sql.append("fair." + Fair.NICK_NAME+" like ?)");
                param.add("%" + taskName.toString() + "%");
            }
            if(taskStatus!=null && StrKit.notBlank(taskStatus.toString())){
                sql.append(" and (");
                if (taskStatus.toString().equals(PARAM_TASK_STATUS_BEFORE)){
                    sql.append("task." + Task.STATUS+"=? and task."+Task.TASK_START_TIME+">?)");
                    param.add(Task.STATUS_SUCCESS);
                    param.add(new Date());
                }else if(taskStatus.toString().equals(PARAM_TASK_STATUS_AFTER)){
                    sql.append("task." + Task.STATUS+"=? and task."+Task.TASK_START_TIME+"<?)");
                    param.add(Task.STATUS_START);
                    param.add(new Date());
                }else if(taskStatus.toString().equals(PARAM_TASK_STATUS_END)){
                    sql.append("task." + Task.STATUS+"=?)");
                    param.add(Task.STATUS_COMPLETED);
                }
            }
            sql.append(" group by task."+Task.ID+" order by task."+Task.ID+" DESC");
        }
        Page<Task> page=taskDao.paginate(pageNum,length,s,sql.toString(),param.toArray());
        List<Task> tasks = page.getList();
        for (Task t:tasks) {
        	//检查假如没有支付
        	String ordersql = "select * from fair_order where obj_id=? and opt_type=?";
			FairOrder order = FairOrder.dao.findFirst(ordersql, t.getInt(Task.ID), FairOrder.OPT_TYPE_TASK);
			boolean isPayed = (order==null)?false:order.getInt(FairOrder.STATUS)==FairOrder.STATUS_PAYED;
        	//检查是否有人接单
			
			ordersql = "select * from fair_task_wh where task_id=? ";
			List<TaskWh> wh = TaskWh.dao.find(ordersql,t.getInt(Task.ID));
			boolean result = (wh.isEmpty() && t.getInt(Task.TASK_TYPE)==Task.EXT_TYPE_ZD)?false:true;
			t.put("hasApply",result);
			t.put("isPayed",isPayed);
            t.put("click",t.getClick());
            t.put("cvs",t.getCvs());
            t.put("status",t.status());
        }
        return page;
    }


    /**
     * 根据网红Id获取相关任务信息
     * @param fairId
     * @return
     */
    public Page<Record> findTaskByFair(int pageNum, int length, int fairId){
//        String sql="select *,(select auth.company_name from "+ Authenticate.dao.tableName()+" auth where auth."+Authenticate.USER_ID+"=task."+Task.CREATE_USER+") as cmp,(select fa.nick_name from "+FairAuth.dao.tableName()+" fa where fa."+FairAuth.ID+"=tw."+TaskWh.WH_ID+") whn " +
//                " ";
//        QueryConstructor queryConstructor = new QueryConstructor();
//        queryConstructor.setCommand("from "+TaskWh.dao.tableName() +" tw left join " + taskDao.tableName() +" task on tw."+TaskWh.TASK_ID+"=task."+Task.ID);
//        queryConstructor.and(new EqualCondition("tw." + TaskWh.DELETETAG, TaskWh.DELETE_STATUS_NO));
//        queryConstructor.and(new EqualCondition("tw." + TaskWh.WH_ID,fairId));
//        String from = queryConstructor.buildSQL(" order by tw.id desc");
//        Page<Task> taskPage= taskDao.paginateByCache(Task.TASK_CACHE_NAME,"fair_task_"+fairId+"_"+pageNum+"_"+length,pageNum,length,sql,from,queryConstructor.getParams().toArray());
//        List<Task> tasks=taskPage.getList();
//        for (Task t:tasks) {
//            t.put("statusString",t.status());
//            t.put("click",t.getClick());
//            t.put("cvs",t.getCvs());
//            t.put("dateString",t.getDateString());
//        }
//        return taskPage;
 
    	Fair fair = Fair.dao.findById(fairId);
    	
    	String sql="select task_wh.*,task.task_title as title, task.id as id,"
    			+ "task.task_start_time as start_time ,task.task_end_time as end_time";
    	QueryConstructor queryConstructor = new QueryConstructor();
    	String from = " from fair_task_wh task_wh,fair_task task where (task_wh.deletetag=0 and task_wh.wh_id=? and task_wh.task_id=task.id) order by task_wh.id desc";
    	queryConstructor.setCommand(from);
    	Page<Record> taskPage= Db.paginate(pageNum,length,sql,from,fairId);
    	List<Record> tasks=taskPage.getList();
    	for (Record t:tasks) {
    	  String start = DateUtils.formateData(t.getDate("start_time"), DateUtils.YYMMdd_FORMAT);
    	  String end = DateUtils.formateData(t.getDate("end_time"), DateUtils.YYMMdd_FORMAT);
          t.set("dateString",start+" ~ "+end);
          Task task = Task.dao.findById(t.getLong("id"));
          t.set("statusString",task.status());
          t.set("whn",fair.getStr(Fair.NICK_NAME));
          t.set("img",fair.getStr(Fair.PHOTO));
      }
    	return taskPage;
    }

    /**
     * 根据ID查询任务详情
      * @param id
     * @return
     */
    public Task findTaskById(String id){
        return taskDao.findById(id);
    }

    public Map<String,Object> isShowQrCode(Task task,long uid){
    	double taskPrice = task.getDouble(Task.TASK_PRICE)*task.getInt(Task.WH_NUMBER);
    	Map<String,Object> resultMap = new HashMap<String,Object>();
    	//假如任务为线下任务，则不出现付款二维码
    	if(task.getInt(Task.TASK_TYPE)==Task.TASK_TYPE_OFLINE){
    		resultMap.put("type", "TASK_OFFLINE");
    		resultMap.put("result",false);
    		return resultMap;
    	}
    	//假如用于余额足够支付当前任务信息，则不弹出支付二维码
    	String sql="select * from fair_user_salary where user_id=? limit 1";
    	FairSalary userSalary = FairSalary.dao.findFirst(sql,uid);
    	double salary = (userSalary==null)?0:(userSalary.getInt("salary")*1.0/100);
    	if(salary>=taskPrice){
    		resultMap.put("type", "ENOUGH_TOPAY");
    		resultMap.put("cost", taskPrice);
    		resultMap.put("salary",userSalary.fee());
    		resultMap.put("result",false);
    		return resultMap;
    	}
    	resultMap.put("type", "QRCODE_TOPAY");
		resultMap.put("result",true);
		return resultMap;
    }
    /**
     * 获取所有停止的任务信息
     * @return
     */
    public List<Task> findStopTask(){
        String sql = "select "+Task.ID+","+Task.TASK_PRICE+","+Task.TASK_TITLE+" from "+taskDao.tableName() + " where to_days("+Task.TASK_END_TIME+")<=to_days(now())-1 and "+Task.STATUS +"!="+Task.STATUS_COMPLETED;
        return taskDao.find(sql);
    }

    /**
     * 判断是否可以创建任务
     * @return
     */
    public Authenticate isCreate(long userId){
        Authenticate auth = authDao.findFirst("select * from fair_user_authenticate where user_id=?",userId);
//        if (auth==null){
//            return false;
//        }else if (auth.getInt(Authenticate.STATUS)==Authenticate.STATUS_SUCCESS){
//            return true;
//        }else{
//            return false;
//        }
        return auth;
    }
    
    /**
     * 获取指定任务数据的价格列表
     * @return
     */
    public List<Dictionary> getPriceList(){
    	return DictionaryDao.ME.readParents(Dictionary.CONST_PRICE_CODE);
    }


    /**
     * 获取任务资源
     * @param taskId
     * @return
     */
    public List<TaskSource> findSourceByTask(int taskId){
        String sql = String.format("select * from %s where %s=? and %s=? order by %s asc",taskSourceDao.tableName(),TaskSource.DELETETAG,TaskSource.TASK_ID,TaskSource.SORT);
        return taskSourceDao.find(sql,TaskSource.DELETE_STATUS_NO,taskId);
    }

    /**
     * 保存排序
     */
    public void saveTaskSource(Map<String,String> sources){
        sources.keySet().forEach(str -> {
            TaskSource ts = new TaskSource();
            ts.set(TaskSource.ID,str);
            ts.set(TaskSource.SORT,sources.get(str));
            ts.update();
        });
    }


    public void infoSomeOneWithoutApply(){
    	String sql = "select * from fair_task where id not in(select task_id from fair_task_wh)"
				+ " and status=? and extension_type=? order by id desc";
		List<Task> task = Task.dao.find(sql,Task.STATUS_SUCCESS,Task.EXT_TYPE_RL);//找出所有没有人接单的认领任务类型的任务
		for (Task each : task) {
			Date createTime = each.getDate(Task.CREATE_TIME);
			Date now = new Date();
			long between = now.getTime()-createTime.getTime();
			User user =User.dao.findById(each.getLong(Task.CREATE_USER));
			if(between>3600*1000 && user!=null){//假如所有创建日期超过1个小时，则发送短信
				System.out.println("发送短信"+each.getStr(Task.TASK_TITLE));
				SendSmsMsg.sendInfoSomeOne(user.getStr(User.MOBILE),each.getStr(Task.TASK_TITLE),SendSmsMsg.TEMPLATE_NOBODY_APPLY);
			}
		}
    }
    
    public void setTaskToStart(){
    	String sql="update "+Task.dao.tableName()+" set status=?  where task_start_time between ? and ? and status=? and deletetag=?";
    	String today = DateUtils.formateData(new Date(), DateUtils.YYMMdd_FORMAT);
    	String start_time = today+" 00:00:00";
    	String end_time = today+" 23:59:59";
    	Db.update(sql,Task.STATUS_START,start_time,end_time,Task.STATUS_SUCCESS,Task.DELETE_STATUS_NO);
    }
    
    public void setTaskToEnd(){
    	String sql="update "+Task.dao.tableName()+" set status=?  where task_end_time between ? and ? and status=? and deletetag=?";
    	String today = DateUtils.formateData(new Date(), DateUtils.YYMMdd_FORMAT);
    	String start_time = today+" 00:00:00";
    	String end_time = today+" 23:59:59";
    	Db.update(sql,Task.STATUS_COMPLETED,start_time,end_time,Task.STATUS_START,Task.DELETE_STATUS_NO);
    }
    
    
    /**
     * 定时任务，定时关闭任务
     */
    public void taskJob(){
        List<Task> tasks = findStopTask();
        for (Task task: tasks) {
            task.set(Task.STATUS,Task.STATUS_COMPLETED);
//            final Float price=task.getFloat(Task.TASK_PRICE);
//            List<Fair> fairs=fairService.findByTaskJob(task.getLong(Task.ID)+"");
//            for (Fair f:fairs) {
//                Double d = f.get(Fair.MONEY);
//                if (d==null){
//                    d=0D;
//                }
//                double sum=d.doubleValue()+price.doubleValue();
//                f.set(Fair.MONEY,sum);
//                f.update();
//                MoneyLog ml = new MoneyLog();
//                ml.set(MoneyLog.WH_ID,f.get(Fair.ID));
//                ml.set(MoneyLog.OPERID,task.getLong(Task.ID));
//                ml.set(MoneyLog.STATE, MoneyLog.STATE_ADD);
//                ml.set(MoneyLog.TYPE,MoneyLog.TYPE_RENWU);
//                ml.set(MoneyLog.MONEY,price.doubleValue());
//                ml.set(MoneyLog.DESCRIPTION,"任务完成："+task.getStr(Task.TASK_TITLE));
//                ml.save();
//            }
            task.update();
        }
    }
    
    public void saveOfflineTask(Task task) throws ValidateException{
    	
    	float price= task.getFloat(Task.TASK_PRICE);
    	String sql="select * from "+Authenticate.dao.tableName()+" where "+Authenticate.USER_ID+" =?";
    	Authenticate auth = Authenticate.dao.findFirst(sql, task.getLong(Task.CREATE_USER));
    	
    	Date start = task.getDate(Task.TASK_START_TIME);
    	Date end = task.getDate(Task.TASK_END_TIME);
    	
    	Date now = new Date();
    	Validator validator = new Validator();
    	int status = (auth==null)?Authenticate.STATUS_FAILURE:auth.getInt(Authenticate.STATUS);
    	validator.addRule(new NumbericMustBeEqual(status, Authenticate.STATUS_SUCCESS, "当前用户资质认证审核未通过，不允许提交任务"));
    	validator.addRule(new StringLengthBetween(task.getStr(Task.TASK_TITLE),"任务标题只能是1-15个字符",1,15));
    	validator.addRule(new StringMustBeRequired(DateUtils.formateData(start, DateUtils.YYMMdd_FORMAT),"任务开始时间不能为空"));
    	validator.addRule(new StringMustBeRequired(DateUtils.formateData(end,DateUtils.YYMMdd_FORMAT),"任务结束时间不能为空"));
    	validator.addRule(new NumbericMustBeGreaterEqualThan(end.getTime(),start.getTime(),"任务结束日期必须在开始日期之后"));
    	validator.addRule(new NumbericMustBeGreaterEqualThan(start.getTime(),now.getTime(),"任务开始时间必须在今天日期之后"));
    	
    	validator.addRule(new NumbericMustBeGreaterEqualThan((int)price,0,"请设置任务酬劳"));
    	validator.addRule(new StringLengthBetween(task.getStr(Task.TASK_DESCRIPTION),"任务描述不能超过400字符",1,400));
    	validator.excute();
    	
    	task.set(Task.H5_TYPE, Task.H5_TYPE_NO);
    	task.set(Task.STATUS, Task.STATUS_APPROVING);
    	task.set(Task.EXTENSION_TYPE, Task.EXT_TYPE_RL);
    	task.save();
    }
    
    
    public boolean getTaskCountMode(){
    	return SysConfigDao.ME.getTaskCountMode();
    }
}
